import React, { useContext, useMemo } from 'react';
import { Button, message, Modal, Space } from 'antd';
import { WorkflowHndTaskBtn } from '@/components/WorkflowCommonCopy';
import { ChangeControllerContext } from '../common/useOAContext';
import { downloadTemplate, getPosIDs, getUserID, printerPDF } from '@/utils';
import { BGflowBusinessType, FSBG_STATE_QC, FSBG_STATE_SHZ } from '../common/Format';
import { SHTG_CHECKSTATE, SHTH_CHECKSTATE, SHZ_CHECKSTATE } from '../../Acommon';
import { RollbackOutlined, QuestionCircleOutlined } from '@ant-design/icons';
import moment from 'moment';
import { history } from 'umi';

export const DetailInfoBottomBtnView: React.FC<any> = ({ position, locationProps }) => {
  /** 引入上下文 */
  const { taskId, styleFixCls, infoObj, curRes, reloadDetail, setInvalidState, setOAMessageRun } = useContext(ChangeControllerContext);

  /** 申请页ref */
  const { confirm } = Modal;

  const allBtnsMemo = useMemo(() => {
    let isShowBtn = {
      revokeBtnDiv: false, //撤销按钮
      flowHandList: {}, //流程办理数据
    };

    /** 处理流程办理数据 */
    const taskType: string = BGflowBusinessType();
    const taskHandInfo: Record<string, any> = infoObj?.paramObject?.[taskType];
    if (
      taskHandInfo &&
      taskHandInfo?.mactivitiTaskList?.length > 0 &&
      taskHandInfo?.mactivitiTaskList?.[0]?.assigneePositionID == getPosIDs() &&
      taskHandInfo?.mactivitiTaskList?.[0]?.assigneeID == getUserID()
    ) {
      isShowBtn['invalBoolBtn'] = true;
      isShowBtn['allBtnDiv'] = true;
      // isShowBtn['flowHandList'] = taskHandInfo?.mactivitiTaskList?.[0];
      isShowBtn['flowHandList'] = infoObj;
    }

    return isShowBtn;
  }, [infoObj]);

  const closeCurrent = () => {
    confirm({
      title: '关闭',
      icon: <QuestionCircleOutlined />,
      content: '确定要关闭当前页吗？',
      okText: '确定',
      okType: 'primary',
      cancelText: '取消',
      onOk() {
        history.push({
          pathname: locationProps?.state?.returnPathName,
          state: {
            delRouterNameSource: history.location.pathname, //必传,要删除的路由名称
            returnPathName: locationProps?.state?.returnPathName,
          },
        });
      },
      onCancel() {},
    });
  };

  return (
    <div className={`${styleFixCls}infoViewOA-bottomBtnBox-spaceItemBox`}>
      <Space>
        {/* 流程节点弹框 */}
        {position == 'top' && (
          <WorkflowHndTaskBtn
            flowDetailInfo={allBtnsMemo?.flowHandList}
            reloadCallback={reloadDetail}
            requestController={'assetApplyStorage'}
            clickSuccessFun={() => {
              setOAMessageRun({
                id: taskId,
                isRemark: 2,
                viewType: 1,
                endTime: moment()?.format('YYYY-MM-DD HH:mm:ss'),
              });
            }}
            flowBusinessType={BGflowBusinessType()}
          />
        )}

        {position == 'top' && infoObj?.checkState == SHTG_CHECKSTATE() ? (
          <Button
            type={'primary'}
            onClick={() => {
              message.success('执行中，请稍后...');
              printerPDF(infoObj?.workflowURL);
            }}
          >
            打印
          </Button>
        ) : null}

        {position == 'top' &&
          curRes['/assetApplyStorage/revoke'] &&
          infoObj?.id &&
          infoObj?.createrID == getUserID() &&
          ((infoObj?.checkState == SHZ_CHECKSTATE() && infoObj?.state == FSBG_STATE_SHZ()) ||
            (infoObj?.checkState == SHTH_CHECKSTATE() && infoObj?.state == FSBG_STATE_SHZ())) && (
            <Button
              type="primary"
              danger
              icon={<RollbackOutlined />}
              onClick={() => {
                setInvalidState({ isShow: true, resObj: infoObj });
              }}
            >
              撤销
            </Button>
          )}
      </Space>

      <Button danger onClick={closeCurrent}>
        关闭
      </Button>
    </div>
  );
};
